package com.egg.basic.controller.web;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egg.basic.entity.dict.DictData;
import com.egg.basic.model.request.DictDataPostOrPut;
import com.egg.basic.model.response.DictDataInfo;
import com.egg.basic.model.response.DictDataPage;
import com.egg.basic.service.dict.IDictDataService;
import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.OptionsResponse;
import com.egg.common.core.model.response.PageData;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典数据表 前端控制器
 * </p>
 *
 * @author east
 * @since 2022-06-08
 */
@Slf4j
@Api(tags = WebController.API_PREFIX + "字典数据表")
@RestController
@RequestMapping("/dict/data")
public class WebDictDataController extends WebController {


    @Autowired
    private IDictDataService dictDataService;


    @ApiOperation(value = "字典数据表新增")
    @PostMapping
    public ResponseData<String> dictDataPost(@RequestBody DictDataPostOrPut param) {
        Long id = dictDataService.add(param);
        return ResponseData.success(id.toString());
    }

    @ApiOperation(value = "字典数据表分页")
    @GetMapping("/page")
    public ResponseData<PageData<DictDataPage>> dictDataPage(
            @ApiParam(value = "页数", required = true) @RequestParam(value = "current", defaultValue = "1") Integer current
            , @ApiParam(value = "页长", required = true) @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
            , @ApiParam(value = "字典类型编码") @RequestParam(value = "dictTypeCode", required = false) String dictTypeCode
            , @ApiParam(value = "名称") @RequestParam(value = "name", required = false) String name
    ) {
        LambdaQueryWrapper<DictData> qw = dictDataService.buildLambdaSortQw();
        if (ObjectUtil.isNotEmpty(dictTypeCode)) {
            qw.eq(DictData::getDictTypeCode, dictTypeCode);
        }
        if (ObjectUtil.isNotEmpty(name)) {
            qw.like(DictData::getName, name);
        }
        Page<DictData> dbPage = dictDataService.page(new Page<>(current, pageSize), qw);
        if (ObjectUtil.isEmpty(dbPage.getRecords())) {
            return ResponseData.success(new PageData<>(dbPage));
        }
        List<DictDataPage> list = JsonUtil.listToList(dbPage.getRecords(), DictDataPage.class);
        return ResponseData.success(new PageData<>(dbPage, list));
    }

    @ApiOperation(value = "类型查询字典类型表下拉选")
    @GetMapping("/option")
    public ResponseData<List<OptionsResponse>> dictDataOptionGet(
            @ApiParam(value = "字典类型表类型", required = true) @RequestParam(value = "dictType") String dictType
    ) {
        List<DictData> dbList = dictDataService.findByDictType(dictType);
        List<OptionsResponse> optionList = new ArrayList<>();
        dbList.forEach(o -> {
            OptionsResponse option = new OptionsResponse();
            option.setValue(o.getCode());
            option.setLabel(o.getName());
            optionList.add(option);
        });
        return ResponseData.success(optionList);
    }

    @ApiOperation(value = "字典数据表详情")
    @GetMapping("/{id}/info")
    public ResponseData<DictDataInfo> dictDataInfo(
            @ApiParam(value = "字典数据表主键ID", required = true) @PathVariable String id
    ) {
        DictData dbInfo = dictDataService.baseGetById(id);
        DictDataInfo objInfo = JsonUtil.objToObj(dbInfo, DictDataInfo.class);
        return ResponseData.success(objInfo);
    }

    @ApiOperation(value = "字典数据表编辑")
    @PutMapping("/{id}")
    public ResponseData<String> dictDataPut(
            @ApiParam(value = "字典数据表主键ID", required = true) @PathVariable String id
            , @RequestBody DictDataPostOrPut edit
    ) {
        dictDataService.edit(id, edit);
        return ResponseData.success(id);
    }

    @ApiOperation(value = "字典数据删除")
    @DeleteMapping
    public ResponseData<String> dictDataDel(@RequestParam Collection<String> ids) {
        dictDataService.baseDel(
                ids.stream().filter(o -> ObjectUtil.isNotEmpty(o)).collect(Collectors.toSet())
        );
        return ResponseData.success();
    }


}

